<template>
	<view class="contont">
		<nav-bar :title="pageInfo.navbarTitle" :iconLeft="true" />
		<view class="content-bgc">
			<view class="b-con">
				<uni-forms labelPosition="left" :model="form" :rules="rules" ref="refForm" labelWidth="100">
					<uni-forms-item label="仓库" name="warehouse" required>
						<uni-data-select mode="underline" placeholder="请选择仓库" v-model="form.warehouse" :localdata="warehouse_list"
							:clear="false" />
					</uni-forms-item>
					<uni-forms-item label="存放类型" name="s_type" required>
						<u-radio-group v-model="form.s_type" placement="row" @change="get_s_list">
							<u-radio label="物料" name="2"></u-radio>
							<view style="width: 50rpx;" />
							<u-radio label="产品" name="1"></u-radio>
						</u-radio-group>
					</uni-forms-item>
					<uni-forms-item :label="s_type_name" name="s_id" required>
						<uni-data-select mode="underline" placeholder="请选择" v-model="form.s_id" :localdata="s_list_select"
							@change="select_s" :clear="false" />
					</uni-forms-item>
					<uni-forms-item label="调整类型" name="change_type" required>
						<u-radio-group v-model="form.change_type" placement="row">
							<u-radio label="增加" name="1"></u-radio>
							<view style="width: 50rpx;" />
							<u-radio label="减少" name="2"></u-radio>
						</u-radio-group>
					</uni-forms-item>
					<uni-forms-item label="调整单位" name="unit" required>
						<uni-data-select mode="underline" placeholder="请选择调整单位" v-model="form.unit" :localdata="unit_list"
							:clear="false" />
					</uni-forms-item>
					<uni-forms-item label="数量" name="num" required>
						<u-number-box v-model="form.num" min="0" />
					</uni-forms-item>
					<uni-forms-item label="备注：" name="desc" required>
						<uni-easyinput type="textarea" v-model="form.desc" placeholder="请输入备注" />
					</uni-forms-item>
				</uni-forms>
			</view>
			<u-button type="primary" text="保存" @click="save" />
		</view>
	</view>
</template>
<script setup>
import { reactive, ref } from 'vue';
import { onLoad } from '@dcloudio/uni-app'
import wareInventoryApi from '../../../api/warehouse/ware_inventory'

//页面标题
const pageInfo = reactive({
	navbarTitle: '库存调整'
});

onLoad(() => {
	get_product_bom_list();
	get_warehouse_list()
})

const refForm = ref(null)
const form = reactive({
	warehouse: null,
	s_type: '2',
	s_id: null,
	change_type: '1',
	unit: '',
	num: 0,
	desc: ''
})
const rules = {
	warehouse: { rules: [{ required: true, type: 'number', errorMessage: '请选择仓库' }] },
	s_id: { rules: [{ required: true, type: 'number', errorMessage: '请选择' }] },
	unit: { rules: [{ required: true, errorMessage: '请选择单位' }] },
	desc: { rules: [{ required: true, errorMessage: '请输入备注' }] },
}

const product_list = ref([])
const product_list_select = ref([])
const bom_list = ref([])
const bom_list_select = ref([])
const s_list = ref([])
const s_list_select = ref([])
const warehouse_list = ref([])
const unit_list = ref([])
const s_type_name = ref('')

function get_s_list(val) {
	if (val == 1) {
		s_list_select.value = product_list_select.value
		s_list.value = product_list.value
		s_type_name.value = '产品'
	} else {
		s_list_select.value = bom_list_select.value
		s_list.value = bom_list.value
		s_type_name.value = '物料'
	}
}
function select_s(val) {
	form.unit = ''
	const s_obj = s_list.value.find(item => item.id === val)
	unit_list.value = [s_obj.unit_main, s_obj.unit_assist_one, s_obj.unit_assist_two].filter(Boolean).map(v => ({
		text: v,
		value: v
	}))
}
function get_product_bom_list() {
	const params = { page: 1, page_size: 99999, if_open: 1 };
	wareInventoryApi.get_product(params).then(res => {
		product_list.value = res.results
		product_list_select.value = res.results.map(v => ({
			text: v.name,
			value: v.id
		}))
		wareInventoryApi.get_bom(params).then(res2 => {
			bom_list.value = res2.results
			bom_list_select.value = res2.results.map(v => ({
				text: v.name,
				value: v.id
			}))
			get_s_list(form.s_type);
		})
	})
}
function get_warehouse_list() {
	const params = { page: 1, page_size: 99999, if_open: 1 };
	wareInventoryApi.get_warehouse(params).then(res => {
		warehouse_list.value = res.results.map(v => ({
			text: v.name,
			value: v.id
		}))
	})
}
//提交操作
function save() {
	refForm.value.validate().then(() => {
		const params = uni.$lodash.cloneDeep(form) // 深拷贝一份form表单
		//修改
		wareInventoryApi.post_stock_control(params).then((res) => {
			uni.$u.toast('调整成功')
			setTimeout(() => {
				backRefsh();
			}, 1000)
		})
	}).catch((error) => {
		uni.$u.toast(error[0].errorMessage);
	})
}
//返回上一级并刷新上一级页面
function backRefsh() {
	uni.navigateBack({
		delta: 1, // 返回的页面层数，1 表示返回上一级
		success: () => {
			uni.$bus.useBusEmit('w_inventory:refresh')
		}
	})
}

</script>

<style lang="scss" scoped></style>